<?php

/**
 * uniBoard Users List
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_users{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Base URL for paginator
	 *
	 * @var string
	 */
	
	private $base_url 		= '';
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language File
		$appCore -> loadLanguageFile('users');
		
		// Can we see users list?
		if ( !$appCore -> user_acl['can_see_users_list'])
		{
			// Set Error
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_access'], $appCore -> lang['users_list'], 0);
			
			// Draw Events
			$appCore -> output -> drawEvents();
		}
		else
		{		
			// Load List Templates
			$templates = new tpl_users_list( $appCore);
			
			// Set basic URL
			$this -> base_url = $appCore -> niceLink('act=users&st=%s', $appCore -> lang['board_nav_users']);
				
			// Search query
			$search_sql = '';
				
			// Search?
			if ( $appCore -> _REQ('search') == 1){
				
				// In-search paginator
				$this -> base_url .= '&search=1';
				
				// Find name?
				if ( strlen($appCore -> _REQ('fn')) > 0)
				{
					// Add to link
					$this -> base_url .= '&fn=' . class_strings::sprintfEnc($appCore -> _REQ('fn')) . '&fnt=' . ((int) $appCore -> _REQ('fnt'));
					
					// Add to sql
					switch ($appCore -> _REQ('fnt'))
					{
						case 0:
							$search_sql .= ' AND u.user_name LIKE \'%' . $appCore -> db -> strEscape($appCore -> _REQ('fn'), true) . '%\'';
						break;
						case 1:
							$search_sql .= ' AND u.user_name LIKE \'%' . $appCore -> db -> strEscape($appCore -> _REQ('fn'), true) . '\'';
						break;
						case 2:
							$search_sql .= ' AND u.user_name LIKE \'' . $appCore -> db -> strEscape($appCore -> _REQ('fn'), true) . '%\'';
						break;
						default:
							$search_sql .= ' AND u.user_name LIKE \'' . $appCore -> db -> strEscape($appCore -> _REQ('fn'), true) . '\'';
						break;
					}
					
				}
				
				// Find group?
				if ( $appCore -> _REQ('fg') > 0)
				{
					// Add to link
					$this -> base_url .= '&fg=' . ((integer) $appCore -> _REQ('fg'));
					
					// Add to sql
					$search_sql .= ' AND gm.group_id = \'' . ((integer) $appCore -> _REQ('fg')) . '\'';
				}
			}
			
			// Template Variables
			$tpl_vars = array(
				'found_users'	=> $this -> appCore -> db -> count( 'users u LEFT JOIN ' . DB_PREFIX . 'user_groups_members gm ON u.user_id = gm.user_id', 'u.user_id > \'0\'' . $search_sql),
				'users_list' 	=> ''
			);
			
			// Users groups
			$users_groups = $appCore -> users -> getUserGroups( false);
			
			// Get Ordering thing
			$order_dir = (bool) $appCore -> _REQ('od');
			$order_type = ((int) $appCore -> _REQ('om') >= 0 && (int) $appCore -> _REQ('om') <= 2 ? (int) $appCore -> _REQ('om') : 0 );
			
			// Set URL
			$this -> base_url .= '&od=' . $order_dir . '&om=' . $order_type;
			
			// Anything found?
			if ( $tpl_vars['found_users'] > 0)
			{
				// Start Paginator
				$paginator = new class_paginator($appCore, $this -> base_url, $tpl_vars['found_users'], $appCore -> conf['users_list_page']);
				
				// Draw It
				$tpl_vars['paginator'] = $paginator -> draw();
						
				// Fix order type
				switch ($order_type)
				{
					case 1:
						$order_type = 'u.user_join_date';
					break;
					
					case 2:
						$order_type = 'u.user_last_date';
					break;
					
					default:
						$order_type = 'u.user_name';
					break;
				}
					
				// Select users
				$appCore -> db -> query('SELECT DISTINCT u.user_id, u.user_name, u.user_avatar_type, u.user_avatar_image, u.user_avatar_width, u.user_avatar_height, u.user_rank, u.user_title, u.user_posts, u.user_join_date, u.user_last_date,
				g.group_id, g.group_name, g.group_prefix, g.group_suffix, g.group_rank
				FROM ' . DB_PREFIX . 'user_groups_members gm
				LEFT JOIN ' . DB_PREFIX . 'users u ON gm.user_id = u.user_id
				LEFT JOIN ' . DB_PREFIX . 'user_groups g ON g.group_id = u.group_id
				WHERE u.user_id > \'0\'' . $search_sql . '
				ORDER BY ' . $order_type . ($order_dir ? ' DESC' : ' ASC') . '
				LIMIT ' . $paginator -> getPosition() . ',' . $appCore -> conf['users_list_page']);
						
				// Groups cache
				$groups_cache = array();
				
				// Draw them
				while ( $user_result = $appCore -> db -> fetch_assoc())
				{
					// Fix user group?
					if ( !isset($groups_cache[$user_result['group_id']]))
					{
						$groups_cache[$user_result['group_id']] = $appCore -> ifString(htmlspecialchars($user_result['group_name']));
					}
					
					// Draw Row
					$tpl_vars['users_list'] .= $templates -> tpl_list_item(array(
						'user_id' 				=> $user_result['user_id'],
						'group_name' 			=> (isset($users_groups[$user_result['group_id']]) ? $users_groups[$user_result['group_id']] : ''),
						'group_prefix' 			=> $user_result['group_prefix'],
						'group_suffix' 			=> $user_result['group_suffix'],
						'user_name' 			=> htmlspecialchars($user_result['user_name']),
						'user_avatar_type' 		=> ($appCore -> user['id'] == $user_result['user_id'] || $appCore -> user_acl['can_see_users_avatars'] ? $user_result['user_avatar_type'] : 0),
						'user_avatar_image'		=> class_users::avatarLocation($user_result['user_avatar_type']) . htmlspecialchars($user_result['user_avatar_image']),
						'user_avatar_width' 	=> $user_result['user_avatar_width'],
						'user_avatar_height' 	=> $user_result['user_avatar_height'],
						'user_title'			=> $appCore -> conf['user_custom_title_lenght'] > 0 && isset($user_result['user_title'][0]) ? htmlspecialchars($user_result['user_title']) : $appCore -> users -> getRankTitle($user_result['user_rank'], $user_result['group_rank'], $user_result['user_posts']),
						'user_posts'			=> $user_result['user_posts'],
						'user_date_join'		=> $user_result['user_join_date'],
						'user_date_last'		=> $user_result['user_last_date'],
					));
				}
				
				// Free result
				$appCore -> db -> freeResult();
			}
			else
			{
				// Set Error
				$appCore -> output -> setEvent( $appCore -> lang['user_list_search_empty'], $appCore -> lang['users_list_search_results'], 0);
			}
				
			// Fill search form
			$tpl_vars['name_search_type'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_list_search_name_0'],
				1 => $appCore -> lang['users_list_search_name_1'],
				2 => $appCore -> lang['users_list_search_name_2'],
				3 => $appCore -> lang['users_list_search_name_3']
			), $appCore -> _REQ('fnt'));
			
			$tpl_vars['name_search'] = $appCore -> _REQ('fn');
			
			$tpl_vars['groups_search'] = $appCore -> drawRawList(array(0 => $appCore -> lang['users_list_search_group_0']) + $users_groups, $appCore -> _REQ('fg'));
	
			// Sorting ops
			$tpl_vars['results_sort_direction'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_list_search_sort_dir_0'],
				1 => $appCore -> lang['users_list_search_sort_dir_1'],
			), $appCore -> _REQ('od'));
			
			$tpl_vars['results_sort_method'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_list_search_sort_0'],
				1 => $appCore -> lang['users_list_search_sort_1'],
				2 => $appCore -> lang['users_list_search_sort_2']
			), $appCore -> _REQ('om'));
				
			// Draw output
			if ( $appCore -> _REQ('search') == 1)
			{
				$appCore -> output -> draw( $appCore -> lang['users_list_search_results'], array(
					array(
						$appCore -> niceLink('act=users',$appCore -> lang['board_nav_users']),
						$appCore -> lang['board_nav_users']
					),
					array(
						$appCore -> niceLink('act=users',$appCore -> lang['board_nav_users']),
						$appCore -> lang['users_list_search_results']
					)
				), $templates -> tpl_list_layout( $tpl_vars));
			}
			else
			{
				$appCore -> output -> draw( $appCore -> lang['users_list'], array(
					array(
						$appCore -> niceLink('act=users',$appCore -> lang['board_nav_users']),
						$appCore -> lang['board_nav_users']
					)
				), $templates -> tpl_list_layout( $tpl_vars));
			}
		}
		
	}
	
}